#include <game.h>

lieu::lieu() : base("lieu")
{
	listStatic = new string<staticMesh*>;
	listAction = new string<actionMesh*>;
}

lieu::~lieu()
{
	delete listAction;
	delete listStatic;
}

int lieu::init(int i,coeur* c,IXMLReader* xmlLieu)
{
	base::init(i,c);
	log(stringc("Lieu xml : ") + i + " loading ...");
	staticMesh* mesh;
	while(xmlLieu && xmlLieu->read())
	{
		if (xmlLieu->getNodeType()==EXN_ELEMENT)
		{
			if (core::stringw("static") == xmlLieu->getNodeName())
			{
				mesh=addStaticMesh(
					stringc(xmlLieu->getAttributeValue(L"model")),
					xmlLieu->getAttributeValueAsInt(L"id")
				);
				log( stringc("New static : id:") + xmlLieu->getAttributeValueAsInt(L"id") + ", model: " + xmlLieu->getAttributeValue(L"model"));
			}
			else if (core::stringw("observation") == xmlLieu->getNodeName())
			{
				mesh=addObservationMesh(
					stringc(xmlLieu->getAttributeValue(L"model")),
					xmlLieu->getAttributeValueAsInt(L"id"),
					xmlLieu->getAttributeValueAsInt(L"combiId")
				);
				log( stringc("New observation : id:") + xmlLieu->getAttributeValueAsInt(L"id") + ", model: " + xmlLieu->getAttributeValue(L"model") + ", combiId: " + xmlLieu->getAttributeValue(L"combiId"));
			}
			else if (core::stringw("prendre") == xmlLieu->getNodeName())
			{
				mesh=addPrendreMesh(
					stringc(xmlLieu->getAttributeValue(L"model")),
					xmlLieu->getAttributeValueAsInt(L"id"),
					xmlLieu->getAttributeValueAsInt(L"combiId"),
					xmlLieu->getAttributeValueAsInt(L"itemId")
				);
				log( stringc("New prendre : id:") + xmlLieu->getAttributeValueAsInt(L"id") + ", model: " + xmlLieu->getAttributeValue(L"model") + ", combiId: " + xmlLieu->getAttributeValue(L"combiId") + ", itemId: " + xmlLieu->getAttributeValue(L"itemId"));
			}
			else if (core::stringw("animate") == xmlLieu->getNodeName())
			{
				mesh=addAnimateMesh(
					stringc(xmlLieu->getAttributeValue(L"model")),
					xmlLieu->getAttributeValueAsInt(L"id"),
					xmlLieu->getAttributeValueAsInt(L"combiId"),
					xmlLieu->getAttributeValueAsInt(L"animId"),
					xmlLieu->getAttributeValueAsInt(L"begin"),
					xmlLieu->getAttributeValueAsInt(L"end")
				);
				log( stringc("New animated : id:") + xmlLieu->getAttributeValueAsInt(L"id") + ", model: " + xmlLieu->getAttributeValue(L"model") + ", combiId: " + xmlLieu->getAttributeValueAsInt(L"combiId") + ", animId: " + xmlLieu->getAttributeValueAsInt(L"animId") + ", begin: " + xmlLieu->getAttributeValueAsInt(L"begin")  + ", end: " + xmlLieu->getAttributeValueAsInt(L"end"));
			}
			else if (core::stringw("position") == xmlLieu->getNodeName())
			{
				mesh->getNode()->setPosition(vector3df(
					xmlLieu->getAttributeValueAsFloat(L"x"),
					xmlLieu->getAttributeValueAsFloat(L"y"),
					xmlLieu->getAttributeValueAsFloat(L"z")
				));
				log( stringc("	Position : (") + xmlLieu->getAttributeValueAsFloat(L"x") + "," + xmlLieu->getAttributeValueAsFloat(L"y") + "," + xmlLieu->getAttributeValueAsFloat(L"z") + ")");
			}
			else if (core::stringw("rotation") == xmlLieu->getNodeName())
			{
				mesh->getNode()->setRotation(vector3df(
					xmlLieu->getAttributeValueAsFloat(L"x"),
					xmlLieu->getAttributeValueAsFloat(L"y"),
					xmlLieu->getAttributeValueAsFloat(L"z")
				));
				log( stringc("	Rotation : (") + xmlLieu->getAttributeValueAsFloat(L"x") + "," + xmlLieu->getAttributeValueAsFloat(L"y") + "," + xmlLieu->getAttributeValueAsFloat(L"z") + ")");
			}
			else if (core::stringw("scale") == xmlLieu->getNodeName())
			{
				mesh->getNode()->setScale(vector3df(
					xmlLieu->getAttributeValueAsFloat(L"x"),
					xmlLieu->getAttributeValueAsFloat(L"y"),
					xmlLieu->getAttributeValueAsFloat(L"z")
				));
				log( stringc("	Scale : (") + xmlLieu->getAttributeValueAsFloat(L"x") + "," + xmlLieu->getAttributeValueAsFloat(L"y") + "," + xmlLieu->getAttributeValueAsFloat(L"z") + ")");
			}
		}
		if (xmlLieu->getNodeType()==EXN_ELEMENT_END)
			if ((core::stringw("static") == xmlLieu->getNodeName())||(core::stringw("observation") == xmlLieu->getNodeName())||(core::stringw("prendre") == xmlLieu->getNodeName()))
				mesh=0;
	}


	return 1;
}

int lieu::update()
{
	return 1;
}

int lieu::close()
{
	log("Closing each staticMesh");
	while (listStatic->size()>0)
	{
		log(stringc("Deleting staticMesh ") + (*listStatic)[0]->getId());
		delete (*listStatic)[0];
		listStatic->erase(0);
	}
	log("Closing each actionMesh");
	while (listAction->size()>0)
	{
		log(stringc("Deleting actionMesh ") + (*listAction)[0]->getId());
		delete (*listAction)[0];
		listAction->erase(0);
	}
	base::close();
	return 1;
}

staticMesh* lieu::addStaticMesh(stringc path,int i)
{
	staticMesh *m = new staticMesh;
	m->init(i,mycore->addMesh(path));
	*listStatic += m;
	log("staticMesh added");
	return m;
}

observationMesh* lieu::addObservationMesh(stringc path, int i, int c)
{
	observationMesh *m = new observationMesh;
	m->init(i,mycore->addMesh(path),c);
	*listAction += m;
	log("observationMesh added");
	return m;
}

prendreMesh* lieu::addPrendreMesh(stringc path, int i, int c, int ite)
{
	prendreMesh *m = new prendreMesh;
	m->init(i,mycore->addMesh(path),c, ite);
	*listAction += m;
	log("prendreMesh added");
	return m;
}

animateMesh* lieu::addAnimateMesh(stringc path, int i, int c, int ite, int b, int e)
{
	animateMesh *m = new animateMesh;
	m->init(i,mycore->addMesh(path),c, ite, b ,e);
	*listAction += m;
	log("animateMesh added");
	return m;
}

staticMesh* lieu::getStaticMeshFromId(int id)
{
	for (unsigned int i=0;i<listStatic->size();i++)
		if ((*listStatic)[i]->getId() == id)
		{
			return (*listStatic)[i];
		}
	return 0;
}

actionMesh* lieu::getActionMesh(int id)
{
	for (unsigned int i=0;i<listAction->size();i++)
		if ((*listAction)[i]->getId() == id)
		{
			return (*listAction)[i];
		}
	return 0;
}

actionMesh* lieu::getActionMesh(ISceneNode* node)
{
	for (unsigned int i=0;i<listAction->size();i++)
		if ((*listAction)[i]->getNode() == node)
		{
			return (*listAction)[i];
		}
	return 0;
}

bool lieu::OnEvent(const SEvent& event)
{
	return false;
}

